//
//  ServiceRootViewController.m
//  YunDongAdmin
//
//  Created by lqq on 2018/9/4.
//  Copyright © 2018年 cloudfighting.com. All rights reserved.
//



typedef NS_ENUM(NSInteger, ServiceRootVCCellType) {
    ServiceRootVCCellType_ZengZhi,//增值服务
    ServiceRootVCCellType_BrandManager,//品牌管理
    ServiceRootVCCellType_GymManager,//门店管理
    ServiceRootVCCellType_GymNotice,//门店公告
    ServiceRootVCCellType_CustomerService,//专属客服
    ServiceRootVCCellType_Share,//分享给好友
    ServiceRootVCCellType_Setting,//设置
};
#import "ServiceRootViewController.h"
#import "CommonImageTableViewCell.h"
#import "BrandManagementController.h"
#import "GymManagermentRootController.h"
#import "SettingViewController.h"
#import "ShareTool.h"
#import "I_Account.h"
#import "NSDate+Extension.h"

@interface ServiceRootViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView *tableHeaderView;
@property (nonatomic, strong) NSArray *dataSource;
@property (nonatomic, assign) long long accountEndDate;
@end

@implementation ServiceRootViewController

#pragma mark Class Method
+ (instancetype)controller
{
    ServiceRootViewController *vc = [UIStoryboard controllerWithStoryBoardIdentify:NSStringFromClass([ServiceRootViewController class]) inStoryBoard:@"Main"];
    return vc;
}

#pragma mark Life Circle
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"服务";
    [self configUI];
    
    [self refreshTableView];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self requestAccountEndDate];
}

#pragma mark Config/UI
- (void)configUI
{
    _tableHeaderView.height = 200 * kDeviceWidth / 750.0;
    _tableView.tableHeaderView = _tableHeaderView;
    _tableView.backgroundColor = TP.controllerBackColor;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle =UITableViewCellSeparatorStyleNone;
    [_tableView registerClass:[CommonImageTableViewCell class] forCellReuseIdentifier:NSStringFromClass([CommonImageTableViewCell class])];
    
    UITapGestureRecognizer *headerViewTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(headerViewClicked)];
    [self.tableHeaderView addGestureRecognizer:headerViewTap];
}

- (void)refreshTableView
{
    if (_accountEndDate > 0) {
        _dataSource = @[@[@(ServiceRootVCCellType_ZengZhi)],@[@(ServiceRootVCCellType_BrandManager),@(ServiceRootVCCellType_GymManager)],@[@(ServiceRootVCCellType_CustomerService),@(ServiceRootVCCellType_Share)],@[@(ServiceRootVCCellType_Setting)]];

    } else {
        _dataSource = @[@[@(ServiceRootVCCellType_BrandManager),@(ServiceRootVCCellType_GymManager)],@[@(ServiceRootVCCellType_CustomerService),@(ServiceRootVCCellType_Share)],@[@(ServiceRootVCCellType_Setting)]];

    }
    [self.tableView reloadData];
}

#pragma mark NET
- (void)requestAccountEndDate
{
    WeakSelf(self);
    [I_Account requestAccountEndDateOnSuccess:^(long long endDate) {
        weakself.accountEndDate = endDate;
        [weakself refreshTableView];
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark Action
- (void)headerViewClicked
{
    YDWebViewController *webVC = [YDWebViewController controllerWithUrl:Url_ForUseAdminApp];
    [self.navigationController pushViewController:webVC animated:YES];
}


#pragma mark UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *arr = _dataSource[section];
    return arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommonImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([CommonImageTableViewCell class])];
    ServiceRootVCCellType cellType = [_dataSource[indexPath.section][indexPath.row] integerValue];
    switch (cellType) {
        case ServiceRootVCCellType_ZengZhi://增值服务
        {
            ServiceZengZhiCell *zengzhiCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ServiceZengZhiCell class])];
//            zengzhiCell.descLabel.text = [NSString stringWithFormat:@""];
            NSDate *endDate = [NSDate dateWithTimeIntervalSince1970:_accountEndDate/1000];
            NSString *endDateStr = [Toolkit stringFromDate:endDate formatterString:@"yyyy-MM-dd"];
//            NSInteger remindDays = [endDate daysLaterThan:[NSDate date]];
            zengzhiCell.descLabel.text = [NSString stringWithFormat:@"%@后到期",endDateStr];

            
            return zengzhiCell;
        }
            break;
        case ServiceRootVCCellType_BrandManager://品牌管理
        {
            [cell refreshCellWithIconName:@"ic_50_pinpaiguanli" leftText:@"健身房信息"];
        }
            break;
        case ServiceRootVCCellType_GymManager://门店管理
        {
            [cell refreshCellWithIconName:@"ic_50_mendianguanli" leftText:@"门店管理"];
        }
            break;
        case ServiceRootVCCellType_GymNotice://门店公告
        {
            [cell refreshCellWithIconName:@"ic_50_mendiangonggao" leftText:@"门店公告"];
        }
            break;
        case ServiceRootVCCellType_CustomerService://专属客服
        {
            [cell refreshCellWithIconName:@"ic_50_zhuanshukefu" leftText:@"专属客服"];
        }
            break;
        case ServiceRootVCCellType_Share://分享给好友
        {
            [cell refreshCellWithIconName:@"ic_50_fenxianggeipengyou" leftText:@"分享给好友"];
        }
            break;
        case ServiceRootVCCellType_Setting://设置
        {
            [cell refreshCellWithIconName:@"ic_50_shezhi" leftText:@"设置"];
        }
            break;
            
        default:
            break;
    }
    
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ServiceRootVCCellType cellType = [_dataSource[indexPath.section][indexPath.row] integerValue];
    switch (cellType) {
        case ServiceRootVCCellType_ZengZhi://增值服务
        {
//            [YDAlertViewController alertViewControllerWithMessage:@"请前往公众号【云动助手】进行开通高级版服务" cancleTitle:@"取消" confimTitle:@"复制公众号名称" cancleBlock:nil confimBlock:^{
//                UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
//                pasteboard.string = @"云动助手";
//                [AV showSuccess:@"公众号【云动助手】已添加到剪切板"];
//            }];
        }
            break;
        case ServiceRootVCCellType_BrandManager://品牌管理
        {
            BrandManagementController *brandManagementVC = [BrandManagementController controller];
            [self.navigationController pushViewController:brandManagementVC animated:YES];
        }
            break;
        case ServiceRootVCCellType_GymManager://门店管理
        {
            GymManagermentRootController *manageStoreVC = [GymManagermentRootController controller];
            [self.navigationController pushViewController:manageStoreVC animated:YES];
        }
            break;
        case ServiceRootVCCellType_GymNotice://门店公告
        {
        }
            break;
        case ServiceRootVCCellType_CustomerService://专属客服
        {
            YDWebViewController *webVC = [YDWebViewController controllerWithUrl:[NSString stringWithFormat:@"https://www.sobot.com/chat/h5/index.html?sysNum=5aa37b4d056f4292886ae54c76950e94&uname=管理：%@",RI.account.brandName]];
            [self.navigationController pushViewController:webVC animated:YES];
        }
            break;
        case ServiceRootVCCellType_Share://分享给好友
        {
            [ShareTool shareWebUrlToPlatorm:SharePlatformType_WechatSession webUrl:Url_For_InviteUseApp title:@"推荐使用云动管理" desc:@"它是一款简单实用的健身工作室管理工具，很适合用于管理门店和教练" thumImage:IMG_NAME(@"ic_launcher_APP") currentController:self];
        }
            break;
        case ServiceRootVCCellType_Setting://设置
        {
            SettingViewController *settingVC = [SettingViewController controller];
            [self.navigationController pushViewController:settingVC animated:YES];
        }
            break;
            
        default:
            break;
    }
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0.01;
    }
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

@end





@implementation ServiceZengZhiCell


@end







